<script>
import { autoLoginUserApi } from './api/user/login'
import { getUserInfoApi } from './api/user/user'
import { getMerchantInfoApi } from './api/merchant/merchant'

export default {
  globalData: {},
  onLaunch: function(options) {
    console.log('App Launch')
    this.update()
    this.autoLogin()
    this.getMerchantInfo()
    this.setRefererScene(options)
  },
  onShow: function() {
    this.cacheLocation()
    console.log('App Show')
  },
  onHide: function() {
    console.log('App Hide')
  },
  methods: {
    update() {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function(res) {
        // 请求完新版本信息的回调
        console.log('请求完新版本信息的回调', res)
      })
      updateManager.onUpdateReady(function() {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })
      updateManager.onUpdateFailed(function() {
        // 新版本下载失败
        console.error('新版本下载失败')
      })
    },
    setRefererScene(options) {
      const params = JSON.parse(JSON.stringify(options))
      this.$store.commit('referer/setScene', params.scene || '')
      if ('scene' in params.query) {
        const _scene = decodeURIComponent(params.query.scene)
        if (_scene.indexOf('=') !== -1) {
          const optionsMap = new Map(_scene.split('&').map(ele => ele.split('=')))
          for (const [key, value] of optionsMap) {
            params.query[key] = value
          }
          this.$store.commit('referer/setData', params.query)
        }
      }
      console.log(params, 'scene')
    },
    autoLogin() {
      const _this = this
      uni.login({
        provider: 'weixin',
        success: function(loginRes) {
          autoLoginUserApi({
            code: loginRes.code
          }).then(response => {
            if (response.data.openId && response.data.openId !== '') {
              uni.setStorage({ key: 'openId', data: response.data.openId })
            }
            if (response.data.unionId && response.data.unionId !== '') {
              uni.setStorage({ key: 'unionId', data: response.data.unionId })
            }
            if (response.data.sid && response.data.sid !== '') {
              uni.setStorageSync('sid', response.data.sid)
              // _this.$store.commit('referer/setSid', response.data.sid)
            }
            if (response.data.token && response.data.token !== '') {
              uni.setStorageSync('token', response.data.token)
              _this.getUserInfo()
            }
          })
        }
      })
    },
    getUserInfo() {
      getUserInfoApi(true, false, false).then(response => {
        uni.setStorage({ key: 'userInfo', data: response.data })
      })
    },
    getMerchantInfo() {
      getMerchantInfoApi(true, false).then(response => {
        uni.setStorage({ key: 'merchantInfo', data: response.data })
      })
    },
    cacheLocation() {
      uni.getSetting({
        success(res) {
          if (res.authSetting['scope.userLocation']) {
            uni.getLocation({
              type: 'gcj02',
              success: function(res) {
                uni.setStorage({ key: 'nowLatitude', data: res.latitude })
                uni.setStorage({ key: 'nowLongitude', data: res.longitude })
              }
            })
          }
        }
      })
    }
  }}
</script>

<style>
.text-ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

body {
  /* background: rgba(245, 246, 249, 1); */
}

.card-box {
  background: white;
  border-radius: 14 rpx;
  padding: 30 rpx;
}
</style>
